package com.wolfsea.scrolledfloatactionbutton
import android.app.Activity
import android.app.Application
import android.content.ComponentCallbacks
import android.content.res.Configuration
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import com.wolfsea.scrolledfloatactionbutton.application.ScrollApplication
import com.wolfsea.scrolledfloatactionbutton.util.ScreenUtil
import kotlinx.android.synthetic.main.activity_text_view_drawable.*

class TextViewDrawableActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setCustomDestiny(this, ScrollApplication.instance)

        setContentView(R.layout.activity_text_view_drawable)

        val textView = AppCompatTextView(this)

        textView.apply {
            gravity = Gravity.CENTER_VERTICAL
            text = "123456"
            setBackgroundColor(resources.getColor(R.color.purple_200))
            width = ViewGroup.LayoutParams.MATCH_PARENT
            height = resources.getDimension(R.dimen.dp_50).toInt()

            setCompoundDrawablesWithIntrinsicBounds(
                null,
                null,
                resources.getDrawable(R.mipmap.ic_launcher_round),
                null
            )

            //同时改变icon和文本的边距
            setPadding(resources.getDimension(R.dimen.dp_135).toInt(), 0, resources.getDimension(R.dimen.dp_130).toInt(), 0)
        }

        root_ll.addView(textView)

//        Log.d("value","size01:" + Resources.getSystem().displayMetrics.widthPixels)
//        Log.d("value","size01:" + Resources.getSystem().displayMetrics.heightPixels)
//        Log.d("value","screen:" + resources.displayMetrics.widthPixels)
//        Log.d("value","screen:" + resources.displayMetrics.heightPixels)

//        Log.d("value","size01:" + resources.displayMetrics.densityDpi)
//        Log.d("value","size01:" + resources.displayMetrics.density)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    companion object {

        private var sNonCompatDestiny: Float = 0.0F
        private var sNonCompatScaledDestiny: Float = 0.0F

        fun setCustomDestiny(activity: Activity, application: Application) {

            val appDm = application.resources.displayMetrics
            if (sNonCompatDestiny == 0F) {

                sNonCompatDestiny = appDm.density
                sNonCompatScaledDestiny = appDm.scaledDensity

                application.registerComponentCallbacks(object : ComponentCallbacks {
                    override fun onConfigurationChanged(newConfig: Configuration) {
                        if (newConfig != null && newConfig.fontScale > 0) {
                            sNonCompatScaledDestiny =
                                application.resources.displayMetrics.scaledDensity
                        }
                    }
                    override fun onLowMemory() {}
                })

                val targetDestiny = appDm.widthPixels / 392F
                val targetScaledDensity =
                    targetDestiny * (sNonCompatScaledDestiny / sNonCompatDestiny)
                val targetDestinyDpi = (160 * targetDestiny).toInt()


                appDm.density = targetDestiny
                appDm.scaledDensity = targetScaledDensity
                appDm.densityDpi = targetDestinyDpi

                val activityDm = activity.resources.displayMetrics
                activityDm.density = targetDestiny
                activityDm.scaledDensity = targetScaledDensity
                appDm.densityDpi = targetDestinyDpi
            }
        }

    }

}